package com.chainyoung.trademarket.mvp.presenter;

import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.bean.UserInfo;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.trademarket.di.module.FindData;
import com.chainyoung.trademarket.entity.IndexApp;
import com.chainyoung.trademarket.entity.IndexData;
import com.chainyoung.trademarket.mvp.contract.FindContract;
import com.chainyoung.trademarket.mvp.contract.IndexContract;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

public class IndexPresenter  extends BasePresenter<IndexContract.Model, IndexContract.View> {

    @Inject
    public IndexPresenter(IndexContract.Model model, IndexContract.View rootView) {
        super(model, rootView);
    }


    public void getIndexData(TreeMap<String, Object> map) {

        addSubscribe(mModel.getIndexApps(map)
                .compose(RxUtils.<IndexData>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<IndexData>(mView) {
                    @Override
                    public void onNext(IndexData data) {
                        mView.onGetIndexAppsSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetIndexAppsSuccess(null);
                                return;
                            }
                        }
                        mView.onGetIndexAppsFailed();
                        super.onError(e);
                    }
                }));
    }

    public void getUserInfo(TreeMap<String, Object> map) {
        addSubscribe(mModel.getUserInfo(map)
                .compose(RxUtils.<UserInfo>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<UserInfo>(mView,false) {
                    @Override
                    public void onNext(UserInfo object) {
                        mView.onGetUserInfoSuccess(object);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


}
